<template>
	<view>
		<view class="top-header">
			<!-- 头部 -->
			<!-- <view class="header">
				<icon type="search" size="16" color="#bbb">
				</icon>
				<input type="text" v-model="keyword" confirm-type="search" @confirm="search">
			</view> -->
			<SearchBar @search='searchHandler' :keyword='keyword'></SearchBar>

			<!-- 过滤菜单 -->
			<view class="filter-menu">
				<view class="active" v-for="(item, index) in 3" :key="index">销量
				</view>
			</view>
		</view>

		<!-- 商品列表 -->
		<view class="goods-list">
			<view class="goods" v-for="(item, index) in goodsList" :key="index" @click="toItem(item.goods_id)">
				<image :src="item.goods_small_logo" alt="">
					<view class="right">
						<view class="goods-name text-line2">{{item.goods_name}}</view>
						<view class="price">￥<text>{{item.goods_price}}</text>.00</view>
					</view>
			</view>
		</view>
		<view class="btm-line" v-if="isLastPage">
			我是有底线的
		</view>
	</view>
</template>
<script>
	import SearchBar from "../../compoents/SearchBar.vue"
	const PAGE_SIZE = 6
	const HISTORY_LIST_KEY = 'history_list'
	export default {
		components: {
			SearchBar
		},
		data() {
			return {
				keyword: '',
				goodsList: [],
				pageNum: 1,
				isLastPage: false,
				isRequesting: false
			}
		},
		onLoad(options) {
			this.keyword = options.catName
			console.log(options.catName)
			this.queryGoodsList()
			this.addHistoryList(this.keyword)
		},
		// 下来会刷新
		onPullDownRefresh() {
			// console.log('ok')
			// this.pageNum = 1
			// this.goodsList = []
			// this.queryGoodsList()
			this.search()
		},
		// 上拉刷新
		onReachBottom() {
			if (!this.isLastPage) {
				this.pageNum++;
				this.queryGoodsList();
			}
		},
		methods: {
			// 关键子搜索
			addHistoryList(keyword) {
				let historyList = uni.getStorageSync(HISTORY_LIST_KEY) || []
				// 关键字应该插入到history数组头部， 并且去重
				historyList.unshift(keyword);
				// 或者用以下...展开运算符也行
				// let _historyList = [inputVal, ...this.historyList]
				historyList = [...new Set(historyList)];
				// 历史搜索改变应该存储到storage
				uni.setStorageSync(HISTORY_LIST_KEY, historyList);
			},
			searchHandler(inputVal) {
				this.keyword = inputVal
				this.search()
				this.addHistoryList(inputVal)
			},
			toItem(goodsId) {
				uni.navigateTo({
					url: '../item/item?goodsId=' + goodsId
				})
			},
			// 输入框搜索
			search() {
				console.log('ok')
				this.pageNum = 1
				this.goodsList = []
				this.queryGoodsList()
			},
			// 根据关键子查询列表
			async queryGoodsList() {
				// 请求前,如果有请求正在处理,等待
				if (this.isRequesting) {
					return;
				}
				// 请求开始前,设置请求中
				this.isRequesting = true;
				let data = await this.$request({
					url: '/api/public/v1/goods/search',
					data: {
						query: this.keyword,
						pagenum: this.pageNum,
						pagesize: PAGE_SIZE
					},
					isShowLoading: false
				})

				// 请求结束后,设置不要请求中
				this.isRequesting = false;
				uni.stopPullDownRefresh()
				// this.goodsList = data.goods
				this.goodsList.push(...data.goods)
				if (data.total === this.goodsList.length) {
					this.isLastPage = true
				}
			}
		}
	}
</script>
<style lang="less">
	.header {
		height: 120rpx;
		padding: 30rpx 16rpx;
		box-sizing: border-box;
		background-color: #eee;
		position: relative;

		icon {
			position: absolute;
			top: 48rpx;
			left: 44rpx;
		}

		input {
			height: 60rpx;
			width: 100%;
			border-radius: 4rpx;
			background-color: #fff;
			padding-left: 80rpx;
			box-sizing: border-box;
		}
	}

	.top-header {
		position: sticky;
		top: 0;
		left: 0;
		right: 0;
		background-color: #fff;
	}

	.header {
		height: 120rpx;
		padding: 30rpx 16rpx;
		box-sizing: border-box;
		background-color: #eee;
		position: relative;

		icon {
			position: absolute;
			top: 48rpx;
			left: 44rpx;
		}

		input {
			height: 60rpx;
			width: 100%;
			border-radius: 4rpx;
			background-color: #fff;
			padding-left: 80rpx;
			box-sizing: border-box;
		}
	}

	.filter-menu {
		display: flex;
		justify-content: space-around;
		align-items: center;
		height: 100rpx;

		view.active {
			color: #EB4450;
		}
	}

	.goods {
		border-top: 1rpx solid #ddd;
		height: 260rpx;
		box-sizing: border-box;
		padding: 0 20rpx;
		display: flex;
		align-items: center;

		image {
			width: 200rpx;
			height: 200rpx;
		}

		.right {
			flex: 1;
			margin-left: 26rpx;

			.price {
				color: #eb4450;
				font-size: 24rpx;
				margin-top: 80rpx;

				text {
					font-size: 36rpx;
				}
			}
		}
	}

	.btm-line {
		text-align: center;
	}
</style>
